package com.renwei.yunlong.activity.me;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.FileCacheUtils;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 设置--页面
 * <p>
 * Created by Administrator on 2017/8/25.
 */

public class SettingActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.btn_out_login)
    Button btnOutLogin;
    @Bind(R.id.rl_change_phone)
    RelativeLayout rlChangePhone;
    @Bind(R.id.rl_change_psd)
    RelativeLayout rlChangePsd;
    @Bind(R.id.rl_clear)
    RelativeLayout rlClear;
    @Bind(R.id.rl_protocol)
    RelativeLayout rlProtocol;
    @Bind(R.id.rl_about)
    RelativeLayout rlAbout;
    @Bind(R.id.tv_size)
    TextView tvSize;
    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        initData();
    }

    private void initData() {
        simpleTileView.setTitle("设置");
        btnOutLogin.setOnClickListener(this);
        rlChangePhone.setOnClickListener(this);
        rlChangePsd.setOnClickListener(this);
        rlClear.setOnClickListener(this);
        rlProtocol.setOnClickListener(this);
        rlProtocol.setVisibility(View.GONE);
        rlAbout.setOnClickListener(this);
        getSize();//获得数据容量
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_change_phone:
                // TODO 更换手机号
                startActivity(new Intent(this, ChangeAccountActivity.class));
                break;
            case R.id.rl_change_psd:
                startActivity(new Intent(this, ChangePasswordActivity.class));
                break;
            case R.id.rl_clear:
                showDialog();
                break;
            case R.id.rl_protocol:
                startActivity(new Intent(this, ProtocolActivity.class));
                break;
            case R.id.rl_about:
                startActivity(new Intent(this, AboutActivity.class));
                break;
            case R.id.btn_out_login:
                exitApp();
                break;
            default:
                break;

        }
    }

    //获得数据容量
    public void getSize() {
        File outCachePath = new File(SettingActivity.this.getCacheDir().getPath() + "/image_manager_disk_cache");
        File cachetemppath = new File(SettingActivity.this.getCacheDir().getPath() + "/temp");
        File cacheFile = new File(SettingActivity.this.getCacheDir().getPath() + "/cacheFile");
        try {
            String outCacheSize = FileCacheUtils.getYunnaCacheSize(outCachePath, cachetemppath, cacheFile);

            tvSize.setText(outCacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //显示dialog
    private void showDialog() {

        View view = View.inflate(getApplicationContext(), R.layout.clear_dialog_layout, null);
        // 实例化 Dialog
        final Dialog dialog = new Dialog(SettingActivity.this);
        // 获取窗口管理器
        Window dialogWindow = dialog.getWindow();
        // 隐藏标题栏
        dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
        Button btnCancel = view.findViewById(R.id.btn_cancel);//取消按钮
        Button btnConfirm = view.findViewById(R.id.btn_confirm);//确认按钮
        // 设置显示的View
        dialogWindow.setContentView(view);
        // 设置显示的位置
        dialogWindow.setGravity(Gravity.CENTER);
        dialog.show();
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String outCachePath = SettingActivity.this.getCacheDir().getPath() + "/image_manager_disk_cache";
                String CacheTempPath = SettingActivity.this.getCacheDir().getPath() + "/temp";
                String cacheFile = SettingActivity.this.getCacheDir().getPath() + "/cacheFile";
                FileCacheUtils.deleteFolderFile(outCachePath, false);
                FileCacheUtils.deleteFolderFile(CacheTempPath, false);
                FileCacheUtils.deleteFolderFile(cacheFile, false);
                dialog.dismiss();
                initData();
            }
        });
    }
}
